gov_races_dem <- c('Arizona', 'California', 'Colorado', 'Connecticut', 'Hawaii', 'Illinois', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'New Mexico', 'New York', 'Oregon', 'Pennsylvania', 'Rhode Island', 'Wisconsin') %>% toupper()

govrace_codes <- statecode %>% filter(state %in% gov_races) %>% select(state_code) %>% as.vector() %>% unlist()

govraces_codes_dems <- statecode %>% filter(state %in% gov_races_dem) %>% select(state_code) %>% as.vector() %>% unlist()

final_gov <- final %>% filter(inputstate %in% as.numeric(govrace_codes))
final_gov <- final_gov %>% mutate(gov_won = case_when(inputstate %in% as.numeric(govraces_codes_dems) & pid2 == "Democrat" ~ 1,
                                                      !(inputstate %in% as.numeric(govraces_codes_dems)) & pid2 == "Democrat" ~ 0,
                                                      inputstate %in% as.numeric(govraces_codes_dems) & pid2 == "Republican" ~ 0,
                                                      !(inputstate %in% as.numeric(govraces_codes_dems)) & pid2 == "Republican" ~ 1))

bb1 <- ggplot()+
  geom_smooth(data = final_gov %>% filter(days < 1, days <=150), aes(days, violence_range,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  geom_smooth(data = final_gov %>% filter(days > -1, days <=150), aes(days, violence_range,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0,0.4))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Support Political Violence')+
  labs(fill = "Governor Candidate", color = 'Governor Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'none')

bb2 <- ggplot()+
  geom_smooth(data = final_gov %>% filter(days < 1, days <=150), aes(days, norms_range,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  geom_smooth(data = final_gov %>% filter(days > -1, days <=150), aes(days, norms_range,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0.2,0.6))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Support Norm Violations')+
  labs(fill = "Governor Candidate", color = 'Governor Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'none')

bb3 <- ggplot()+
  geom_smooth(data = final_gov %>% filter(days < 1, days <=150), aes(days, affpol,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  geom_smooth(data = final_gov %>% filter(days > -1, days <=150), aes(days, affpol,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess')+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(20,80))+
  scale_x_continuous(breaks = seq(-50,150,10))+
  xlab('Days before/after 2022 Election')+
  ylab('Affective Polarization')+
  labs(fill = "Governor Candidate", color = 'Governor Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'bottom')


final_figure_win_gov <- suppressMessages(ggpubr::ggarrange(bb1,bb2,bb3, ncol = 1))
suppressMessages(print(final_figure_win_gov))
#ggsave(final_figure_win_gov, file = 'final_figure_win_gov.png', units = 'in', height = 8, width = 10)